package org.amphiprion.montrictracv4.thread;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.montrictracv4.ApplicationConstants;
import org.amphiprion.montrictracv4.web.adapter.ImageAndLabelListItem;
import org.amphiprion.montrictracv4.web.model.Member;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class MemberCompletion implements Runnable {
	private volatile boolean stop = false;

	private MemberCompletionHandler handler;
	private String text;

	public MemberCompletion(String text, MemberCompletionHandler handler) {
		this.text = text;
		this.handler = handler;
	}

	@Override
	public void run() {
		try {
			// instantiates httpclient to make request
			DefaultHttpClient httpclient = new DefaultHttpClient();

			// url with the post data
			HttpPost httpost = new HttpPost(
					ApplicationConstants.URL_MEMBER_COMPLETION);

			List<NameValuePair> pairs = new ArrayList<NameValuePair>();
			pairs.add(new BasicNameValuePair("term", text));
			httpost.setEntity(new UrlEncodedFormEntity(pairs));
			// Handles what is returned from the page
			ResponseHandler responseHandler = new BasicResponseHandler();
			String result = httpclient.execute(httpost, responseHandler);

			// InputStream localInputStream = response.getEntity().getContent();
			// BufferedReader localBufferedReader = new BufferedReader(new
			// InputStreamReader(localInputStream, "utf-8"));
			// String result = localBufferedReader.readLine();
			// localInputStream.close();

			JSONArray jArray = new JSONArray(result);
			List<ImageAndLabelListItem> members = new ArrayList<ImageAndLabelListItem>();
			for (int i = 0; i < jArray.length(); i++) {
				Member m = new Member();
				JSONObject o = jArray.getJSONObject(i);
				m.id = o.getInt("value");
				m.name = o.getString("label");
				m.image = o.getString("image");
				m.mur = ApplicationConstants.URL_ROOT + o.getString("link");
				int p1 = m.image.indexOf("src=\"");
				int p2 = m.image.indexOf("\"", p1 + 5);
				m.image = m.image.substring(p1 + 5, p2);
				if (m.image.startsWith("/")) {
					m.image = ApplicationConstants.URL_ROOT + m.image;
				}
				// Log.d(ApplicationConstants.PACKAGE, "member:" + m.mur);
				members.add(m);
				if (stop) {
					break;
				}
			}

			if (!stop) {
				handler.listRetrieved(members);
			}
		} catch (Throwable e) {
			Log.e("TAG", "", e);
		}
	}

	public void cancel() {
		stop = true;
	}

	public interface MemberCompletionHandler {
		void listRetrieved(List<ImageAndLabelListItem> members);
	}
}
